package com.grom.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.dispacher.context.ContextHolder;
import com.grom.po.Security;
import com.grom.service.SecurityService;
import com.grom.util.JsonUtil;

public class SecurityAction {
	public void gotoList() {
		List<Security> allSecuritys = SecurityService.instance.getAllSecurity();
		ContextHolder.setRequestAttribute("allSecuritys", allSecuritys);
		ContextHolder.forward("/WEB-INF/jsp/security/update.jsp");
	}

	public void doSave() {
		Security security = (Security) ContextHolder.initFromTheRequest(Security.class);
		if (security.getID() == null || security.getID().trim().equals("")) {
			SecurityService.instance.add(security);
		} else {
			SecurityService.instance.update(security);
			;
		}
		ContextHolder.sendRedirect(ContextHolder.findInstance().getContextUri()
				+ "/SecurityAction/gotoList.do");
	}
	public void doDelete() {
		String id = ContextHolder.findInstance().getRequest()
				.getParameter("deleteId");
		System.out.println("| SecurityAction | doDelete | id | " + id);
		Security security = new Security();
		security.setID(id);
		SecurityService.instance.delete(security);
		ContextHolder.sendRedirect(ContextHolder.findInstance().getContextUri()
				+ "/SecurityAction/gotoList.do");
	}

	public void conditionSelect() {
		String securityName_s = ContextHolder.findInstance().getRequest()
				.getParameter("securityName_s");
		String sql = "select * from security where securityName like '%"
				+ securityName_s + "%' ";
		List<Security> allSecuritys = SecurityService.instance.selectByCondition(sql);
		ContextHolder.setRequestAttribute("allSecuritys", allSecuritys);
		ContextHolder.forward("/WEB-INF/jsp/security/update.jsp");
	}
	
	public void getDetail() {
		String id = ContextHolder.findInstance().getRequest()
				.getParameter("itemid");
		Security security = SecurityService.instance.getSecurityById(id);
		String json = JsonUtil.parseToJson(security);
		HttpServletResponse response = ContextHolder.findInstance()
				.getResponse();
		try {
			PrintWriter out = response.getWriter();
			out.write("security=" + json);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
